#!/bin/sh

echo "Preparing to install HyperHDR..."

# search for users in system, returns first entry
FOUND_USR=`who | grep -o -m1 '^\w*\b'` || "root"

# stop running daemon before we install
if pgrep hyperhdr > /dev/null 2>&1
then
	if grep -m1 systemd /proc/1/comm > /dev/null
	then
		echo "Stopping HyperHDR daemon: systemd"
		# systemd
		systemctl stop hyperhdr"@${FOUND_USR}" 2> /dev/null

	elif [ -e /sbin/initctl ]
	then
		echo "Stopping HyperHDR daemon: upstart"
		# upstart
		initctl stop hyperhdr

	else
		echo "Stopping HyperHDR daemon: sysV"
		# sysV
		service hyperhdr stop 2>/dev/null
	fi
fi

# In case we don't use a service kill all instances
killall hyperhdr 2> /dev/null

# overwrite last return code
exit 0
